//
//  ViewPlayer.m
//  练习11
//
//  Created by 张琦 on 15/11/23.
//  Copyright © 2015年 张琦. All rights reserved.
//

#import "ViewPlayer.h"
#import "UIImageView+WebCache.h"
#import "MusicPlay.h"
@interface ViewPlayer ()
@property (nonatomic, retain) UIImageView *imageTitle;
@property (nonatomic, retain) UILabel *labelTitle;
@property (nonatomic, retain) UIButton *butonPlay;
@property (nonatomic, retain) UIButton *buttonList;
@end
@implementation ViewPlayer
- (void)dealloc
{
    [_imageTitle release];
    [_labelTitle release];
    [_butonPlay release];
    [_buttonList release];
    [super dealloc];
}

+ (instancetype)shareViewPlayer
{
    static ViewPlayer *viewPlayer = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        viewPlayer = [[ViewPlayer alloc] init];
        [viewPlayer createView];
    });
    return viewPlayer;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        // 初始化的时间注册一个通知，用来处理歌曲是否改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handeChange:) name:@"ViewChange" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleStopOrPlay:) name:@"ViewStopOrPlay" object:nil];
    }
    return self;
}
// 处理VC中是暂停还是播放
- (void)handleStopOrPlay:(NSNotification *)noti
{
    if ([[noti.userInfo objectForKey:@"play"] isEqualToString:@"YES"]) {
        self.butonPlay.selected = NO;  // 播放
    } else {
        self.butonPlay.selected = YES;
    }
}
// 改变view的图片和标题
- (void)handeChange:(NSNotification *)noti
{
    self.labelTitle.text = [noti.userInfo objectForKey:@"name"];
    [self.imageTitle sd_setImageWithURL:[NSURL URLWithString:[noti.userInfo objectForKey:@"image"]]];
    self.butonPlay.selected = NO; // 设置按钮状态为播放
    [[NSNotificationCenter defaultCenter] postNotificationName:@"buttonChange" object:nil userInfo:@{@"play":@"YES"}];
}
- (void)handleButtonPlay:(UIButton *)button    
{
    self.butonPlay.selected = !self.butonPlay.selected;
    if (self.butonPlay.selected) {
        [[MusicPlay shareMusicPlay] pauseSong];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"buttonChange" object:nil userInfo:@{@"play":@"NO"}];
    } else {
        [[MusicPlay shareMusicPlay] playSong];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"buttonChange" object:nil userInfo:@{@"play":@"YES"}];

    }
}
- (void)createView
{
    self.imageTitle = [[UIImageView alloc] init];
    self.imageTitle.backgroundColor = [UIColor cyanColor];
    [self addSubview:self.imageTitle];
    [_imageTitle release];
    
    self.labelTitle = [[UILabel alloc] init];
    self.labelTitle.backgroundColor = [UIColor cyanColor];
    [self addSubview:self.labelTitle];
    [_labelTitle release];
    
    self.butonPlay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.butonPlay.backgroundColor = [UIColor cyanColor];
    [self addSubview:self.butonPlay];
    
    self.buttonList = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonList.backgroundColor = [UIColor cyanColor];
    [self addSubview:self.buttonList];
    
    // 设置属性
    [self.butonPlay setBackgroundImage:[UIImage imageNamed:@"iconfont-bofang1"] forState:UIControlStateNormal];  //播放
    [self.butonPlay setBackgroundImage:[UIImage imageNamed:@"iconfont-bofang"] forState:UIControlStateSelected]; // 暂停
    self.butonPlay.selected = YES;   // 默认是暂停
    [self.butonPlay addTarget:self action:@selector(handleButtonPlay:) forControlEvents:UIControlEventTouchUpInside];
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageTitle.frame = CGRectMake(10, 5, 50, 50);
    self.labelTitle.frame = CGRectMake(70, 5, 200, 50);
    self.butonPlay.frame = CGRectMake(300, 5, 50, 50);
    
    self.imageTitle.layer.masksToBounds = YES;
    self.imageTitle.layer.cornerRadius = 25;
    self.butonPlay.layer.masksToBounds = YES;
    self.butonPlay.layer.cornerRadius = 25;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
